Imports NUnit.Framework
Imports pixolut.BizBlox
''' -----------------------------------------------------------------------------
''' Project	 : BizBloxTests
''' Class	 : BizBlox.DOTests
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' 
''' </summary>
''' <remarks>
''' NOTE: tests require non translated characters, thats why we use x and v in tests.
''' See UnicodeTranslator...
''' </remarks>
''' <history>
''' 	[cinj]	22/07/2004	Created
''' </history>
''' -----------------------------------------------------------------------------
<TestFixture()> Public Class SearchBuilderTests
    Public Sub New()
        MyBase.New()
    End Sub

    <TestFixtureSetUp()> Public Sub GlobalInit()
        '        Setup.CreateDataSet()
    End Sub

    <TestFixtureTearDown()> Public Sub GlobalDestruct()
        '       Setup.ClearDataSet()
    End Sub

    <SetUp()> Public Sub LocalInit()

    End Sub

    <TearDown()> Public Sub LocalDestruct()

    End Sub

    <Test(), Ignore("This test is performed in SQLGrouperTest")> Public Sub testRender()
        'we can only assert the attributes of the query
        Dim sb As New SearchBuilder("x")
        Dim out As String = sb.Render("testTable", "testColumn")
        Assert.IsTrue(out.IndexOf("(testTable.testColumn LIKE '%x%')") > -1)
        Assert.IsTrue(out.IndexOf("(testTable.testColumn LIKE '%x')") > -1)
        Assert.IsTrue(out.IndexOf("(testTable.testColumn LIKE 'x%')") > -1)
        Assert.IsTrue(out.IndexOf("(testTable.testColumn LIKE 'x')") > -1)
        Console.WriteLine(out)
    End Sub

    <Test()> Public Sub testRenderStrict()
        Dim sb As New SearchBuilder("starts with x ends with v dostrict")
        Dim out As String = sb.Render("testTable", "testColumn")
        Console.WriteLine(out)
        Assert.IsTrue(out.IndexOf(" AND ") > -1)
    End Sub

    <Test()> Public Sub testRenderNotStrict()
        Dim sb As New SearchBuilder("starts with x ends with v")
        Dim out As String = sb.Render("testTable", "testColumn")
        Console.WriteLine(out)
        Assert.IsTrue(out.IndexOf(" OR ") > -1)
    End Sub

    <Test()> Public Sub testRenderStartQuotes()
        Dim sb As New SearchBuilder("starts with ""x""")
        Dim out As String = sb.Render("testTable", "testColumn")
        Console.WriteLine(out)
        Assert.IsTrue(out.IndexOf("x ") > -1)
    End Sub

    <Test()> Public Sub testRenderEndQuotes()
        Dim sb As New SearchBuilder("ends with ""x""")
        Dim out As String = sb.Render("testTable", "testColumn")
        Console.WriteLine(out)
        Assert.IsTrue(out.IndexOf(" x") > -1)
    End Sub

    <Test()> Public Sub testRenderMidQuotes()
        Dim sb As New SearchBuilder("""x""")
        Dim out As String = sb.Render("testTable", "testColumn")
        Console.WriteLine(out)
        Assert.IsTrue(out.IndexOf(" x ") > -1)
    End Sub

    <Test()> Public Sub testRenderNoQuotes()
        Dim sb As New SearchBuilder("x")
        Dim out As String = sb.Render("testTable", "testColumn")
        Console.WriteLine(out)
        Assert.IsFalse(out.IndexOf(" x ") > -1)
    End Sub

    <Test()> Public Sub testRenderFailNoQuery()
        Dim sb As New SearchBuilder
        Dim out As String = sb.Render("testTable", "testColumn")
        Assert.AreEqual(out, "")
    End Sub

    <Test()> Public Sub testIsSearchSet()
        Dim sb As New SearchBuilder("starts with A nocase dostrict")
        Assert.AreEqual(sb.IsSearchSet, True)
    End Sub

    <Test()> Public Sub testIsSearchSetFail()
        Dim sb As New SearchBuilder
        Assert.AreEqual(sb.IsSearchSet, False)
    End Sub


#Region "Validate parser"
    <Test()> Public Sub testParseStrict()
        Dim sb As New SearchBuilder("starts with A nocase dostrict")
        Assert.AreEqual(sb.StartsWithExact, "A")
        Assert.AreEqual(sb.Strict, True)
    End Sub

    <Test()> Public Sub testParseNoStrict()
        Dim sb As New SearchBuilder("starts with A nocase ")
        Assert.AreEqual(sb.StartsWithExact, "A")
        Assert.AreEqual(sb.Strict, False)
    End Sub

    <Test()> Public Sub testParseStartsWithCaseSens()
        Dim sb As New SearchBuilder("starts with A nocase")
        Assert.AreEqual(sb.StartsWithExact, "A")
        Assert.AreEqual(sb.CaseSensitive, True)
    End Sub

    <Test()> Public Sub testParseStartsWithNoCaseSens()
        Dim sb As New SearchBuilder("starts with A")
        Assert.AreEqual(sb.StartsWithExact, "A")
        Assert.AreEqual(sb.CaseSensitive, False)
    End Sub

    <Test()> Public Sub testParseStartsWith()
        Dim sb As New SearchBuilder("starts with a")
        Assert.AreEqual(sb.StartsWithExact, "a")
    End Sub

    <Test()> Public Sub testParseEndsWith()
        Dim sb As New SearchBuilder("ends with a")
        Assert.AreEqual(sb.EndsWithExact, "a")
    End Sub

    <Test()> Public Sub testParseAnyWords()
        Dim sb As New SearchBuilder("any words a b")
        Console.WriteLine(sb.Render(New Student().TableName, Student.Columns.Name.ToString))
        Assert.AreEqual(sb.ContainsAny, "a b")
    End Sub

    <Test()> Public Sub testParseExactPhrase()
        Dim sb As New SearchBuilder("the exact phrase a b")
        Assert.AreEqual(sb.ContainsExact, "a b")
    End Sub

    <Test()> Public Sub testParseAllWords()
        Dim sb As New SearchBuilder("all words a b")
        Assert.AreEqual(sb.ContainsAll, "a b")
    End Sub

    <Test()> Public Sub testParseDateFrom()
        Dim sb As New SearchBuilder("date from 2004-11-21")
        Assert.IsTrue(sb.StartDate.StartsWith("2004-11-21"))
    End Sub

    <Test()> Public Sub testParseDateTo()
        Dim sb As New SearchBuilder("date to 2004-11-21")
        Assert.IsTrue(sb.EndDate.StartsWith("2004-11-21"))
    End Sub

    <Test()> Public Sub testParseDateFail()
        Dim sb As New SearchBuilder("date to blah")
        Assert.IsTrue(sb.EndDate.Length = 0, "invalid date should be ignored.")
    End Sub

    <Test()> Public Sub testParseWithoutAll()
        Dim sb As New SearchBuilder("without all a")
        Assert.AreEqual(sb.NotContainsAll, "a")
    End Sub

    <Test()> Public Sub testParseWithoutExact()
        Dim sb As New SearchBuilder("without the exact phrase a")
        Assert.AreEqual(sb.NotContainsExact, "a")
    End Sub

    <Test()> Public Sub testParseWithoutAny()
        Dim sb As New SearchBuilder("without any words a b")
        Assert.AreEqual(sb.NotContainsAny, "a b")
    End Sub

#End Region



End Class
